package com.tuanzi.loan.business.service.settlement.impl;

import com.tuanzi.loan.business.entity.settlement.FirstpartyDataChangeRecord;
import com.tuanzi.loan.business.entity.settlement.JfSltBackData;
import com.tuanzi.loan.business.repository.settlement.FirstpartyDataChangeRecordRepository;
import com.tuanzi.loan.business.service.settlement.FirstpartyDataChangeRecordService;
import com.tuanzi.loan.business.service.settlement.JfBackDataService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.settlement.FirstpartyDataChangeRecordInfoRequest;
import com.tuanzi.loan.vo.request.settlement.FirstpartyDataChangeRecordRequest;
import com.tuanzi.loan.vo.response.settlement.FirstpartyDataChangeRecordResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-01-18 10:15
 */
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class FirstpartyDataChangeRecordServiceImpl extends BaseAuditServiceImpl<FirstpartyDataChangeRecord,Long> implements FirstpartyDataChangeRecordService {
    @Autowired
    private FirstpartyDataChangeRecordRepository firstpartyDataChangeRecordRepository;

    @Autowired
    private JfBackDataService jfBackDataService;
    @Override
    protected JPAAuditRepository<FirstpartyDataChangeRecord, Long> getRepository() {
        return firstpartyDataChangeRecordRepository;
    }

    @Override
    public Page<FirstpartyDataChangeRecordResponse> list(PageSearcher<FirstpartyDataChangeRecordRequest> pageSearcher) {
        return firstpartyDataChangeRecordRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public List<FirstpartyDataChangeRecord> findFirstpartyInfo(FirstpartyDataChangeRecordInfoRequest searchRequest) {
        return jfBackDataService.findBySettlementTimeAndCompanyId(
                searchRequest.getStartTime(),searchRequest.getEndTime(),
                searchRequest.getCompanyId());
    }

    @Override
    public void saveDataChangeRecord(List<FirstpartyDataChangeRecord> recordList) {
        recordList.forEach(record -> {
            if (record.getIsEdit() == 1 ){
                saveOrUpdate(record);
                //更新甲方数据表
                JfSltBackData jfSltBackData = jfBackDataService.findOne(record.getJiafangId());
                Integer copMode = record.getCopMode();
                //cpa合作模式
                if (copMode == 0){
                    jfSltBackData.setMarkChannel(record.getNewSpreadChannel()==null?
                            jfSltBackData.getMarkChannel():record.getNewSpreadChannel());
                    jfSltBackData.setRegisterNum(record.getNewRegisterCount()==null?
                            jfSltBackData.getRegisterNum():record.getNewRegisterCount());
                    jfSltBackData.setSltMoney(record.getNewSettlementAmount()==null?
                            jfSltBackData.getSltMoney():record.getNewSettlementAmount());
                }
                //cps合作模式
                else if (copMode == 1){
                    jfSltBackData.setMarkChannel(record.getNewSpreadChannel()==null?
                            jfSltBackData.getMarkChannel():record.getNewSpreadChannel());
                    jfSltBackData.setLendMoney(record.getNewLoanAmount()==null?
                            jfSltBackData.getLendMoney():record.getNewLoanAmount());
                    jfSltBackData.setSltMoney(record.getNewSettlementAmount()==null?
                            jfSltBackData.getSltMoney():record.getNewSettlementAmount());
                }
                //cpc合作模式
                else if (copMode ==2){
                    jfSltBackData.setMarkChannel(record.getNewSpreadChannel()==null?
                            jfSltBackData.getMarkChannel():record.getNewSpreadChannel());
                    jfSltBackData.setClickNum(record.getNewClickCount()==null?
                            jfSltBackData.getClickNum():record.getNewClickCount());
                    jfSltBackData.setSltMoney(record.getNewSettlementAmount()==null?
                            jfSltBackData.getSltMoney():record.getNewSettlementAmount());
                }
                //cpa+cps合作模式
                else if (copMode == 3){
                    jfSltBackData.setMarkChannel(record.getNewSpreadChannel()==null?
                            jfSltBackData.getMarkChannel():record.getNewSpreadChannel());
                    jfSltBackData.setRegisterNum(record.getNewRegisterCount()==null?
                            jfSltBackData.getRegisterNum():record.getNewRegisterCount());
                    jfSltBackData.setLendMoney(record.getNewLoanAmount()==null?
                            jfSltBackData.getLendMoney():record.getNewLoanAmount());
                    jfSltBackData.setSltMoney(record.getNewSettlementAmount()==null?
                            jfSltBackData.getSltMoney():record.getNewSettlementAmount());
                }
                jfBackDataService.saveOrUpdate(jfSltBackData);
            }
        });

    }


}
